﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Domotica.Gazing
{
    /// <summary>
    /// Interaction logic for ConfigWindow.xaml
    /// </summary>
    public partial class ConfigWindow : Window
    {
        public ConfigWindow()
        {
            InitializeComponent();

            SetListBoxes();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            GetConfig();
            _Config.Save();
            this.Close();
        }

        private Config _Config;

        public void SetConfig(Config config)
        {
            _Config = config;

            SetValue(cbxSmoothing, _Config.Smoothing);
            SetValue(cbxHorizontalGain, _Config.HorizontalGain);
            SetValue(cbxVerticalGain, _Config.VerticalGain);
            cbReverseHorizon.IsChecked = _Config.ReverseHorizon;
            SetValue(_Config.CameraNumber, _Config.CameraName);
        }

        public Config GetConfig()
        {
            _Config.Smoothing = GetValue(cbxSmoothing);
            _Config.HorizontalGain = GetValue(cbxHorizontalGain);
            _Config.VerticalGain = GetValue(cbxVerticalGain);
            _Config.ReverseHorizon = cbReverseHorizon.IsChecked.Value;
            GetValue(_Config);
            return _Config;
        }

        public void SetValue(int number, string name)
        {
            foreach (object c in cbxCamera.Items)
            {
                var camera = c as Camera;
                if (camera != null)
                {
                    if (camera.Number == number)
                    {
                        cbxCamera.SelectedItem = c;
                        return;
                    }
                }
            }
        }

        public void GetValue(Config config)
        {
            var camera = cbxCamera.SelectedItem as Camera;
            if (camera != null)
            {
                config.CameraNumber = camera.Number;
                config.CameraName = camera.Name;
            }
        }

        private void SetValue(ComboBox cbx, float value)
        {
            foreach (object c in cbx.Items)
            {
                ConfigValue cv = c as ConfigValue;
                if (cv != null)
                {
                    if (cv.Value <= value)
                    {
                        cbx.SelectedItem = cv;
                        return;
                    }
                }
            }
        }

        private float GetValue(ComboBox cbx)
        {
            ConfigValue cv = cbx.SelectedItem as ConfigValue;
            if (cv == null)
            {
                cv = cbx.Items[0] as ConfigValue;
            }
            if (cv == null)
            {
                return 0;
            }
            return cv.Value;
        }

        private void SetListBoxes()
        {
            cbxSmoothing.Items.Add(new ConfigValue("None", 1f));

            cbxSmoothing.Items.Add(new ConfigValue("Very Low", 0.95f));
            cbxSmoothing.Items.Add(new ConfigValue("Low", 0.8f));
            cbxSmoothing.Items.Add(new ConfigValue("Medium", 0.65f));
            cbxSmoothing.Items.Add(new ConfigValue("Medium High", 0.5f));
            cbxSmoothing.Items.Add(new ConfigValue("High", 3f));
            cbxSmoothing.Items.Add(new ConfigValue("Very High", 0.15f));
            cbxSmoothing.Items.Add(new ConfigValue("Extreme", 0.05f));
                    
            cbxHorizontalGain.Items.Add(new ConfigValue("Very Low", 3f));
            cbxHorizontalGain.Items.Add(new ConfigValue("Low", 4.5f));
            cbxHorizontalGain.Items.Add(new ConfigValue("Medium", 6f));
            cbxHorizontalGain.Items.Add(new ConfigValue("Medium High", 7.5f));
            cbxHorizontalGain.Items.Add(new ConfigValue("High", 9f));
            cbxHorizontalGain.Items.Add(new ConfigValue("Very High", 10.5f));
            cbxHorizontalGain.Items.Add(new ConfigValue("Extreme", 12f));

            cbxVerticalGain.Items.Add(new ConfigValue("Very Low", 3f));
            cbxVerticalGain.Items.Add(new ConfigValue("Low", 4.5f));
            cbxVerticalGain.Items.Add(new ConfigValue("Medium", 6f));
            cbxVerticalGain.Items.Add(new ConfigValue("Medium High", 7.5f));
            cbxVerticalGain.Items.Add(new ConfigValue("High", 9f));
            cbxVerticalGain.Items.Add(new ConfigValue("Very High", 10.5f));
            cbxVerticalGain.Items.Add(new ConfigValue("Extreme", 12f));

            CameraList list = Camera.GetCameraList();
            foreach (Camera camera in list)
            {
                cbxCamera.Items.Add(camera);
            }
        }
    }

    internal class ConfigValue
    {
        public ConfigValue(string title, float value)
        {
            Title = title;
            Value = value;
        }

        public string Title { get; set; }
        public float Value { get; set; }

        public override string ToString()
        {
            return Title;
        }
    }

}
